﻿//using System;
//using System.Collections.Generic;
//using System.Diagnostics;
//using System.Linq;
//using System.Text;

//namespace IAOI.Platform.Common.Diagnostics
//{
//    /// <summary>
//    /// 
//    /// **Service 성능 카운터 **
//    /// Calls , 이 서비스에 대한 호출 수입니다. 
//    /// Calls Duration
//    /// Calls Per Second
//    /// Calls Outstanding
//    /// Calls Failed
//    /// Calls Failed Per Second
//    /// Calls Faulted
//    /// Calls Faulted Per Second
//    /// Calls Duration
//    /// 
//    /// 
//    /// **DB Componet 성능 카운터 발생**
//    /// Transaction 
//    /// Transaction Duration
//    /// Transaction Exception
//    /// Transaction Exception Per Second
//    /// http://msdn.microsoft.com/ko-kr/library/ms731087.aspx
//    /// </summary>
//    public class PerformanceCounterUtil
//    {

//        public static Dictionary<PerformanceCounterKind, PerformanceCounter> InstancePerformanceCounterList = new Dictionary<PerformanceCounterKind, PerformanceCounter>();
//        public static Dictionary<PerformanceCounterKind, PerformanceCounter> TotalPerformanceCounterList = new Dictionary<PerformanceCounterKind, PerformanceCounter>(); 

//        //Default, 
//        //ServiceOnly, 
//        //All,
//        //Off
 
//        /// <summary>
//        /// Close All Counter Section and Release Memory
//        /// </summary>
//        public static void Close()
//        {
//            //foreach (PerformanceCounter counter in CounterList)
//            //{
//            //    counter.Close();
//            //    counter.Dispose();
//            //}
//        }

//        /// <summary>
//        /// Remove All HSF Counter Infomation - called when BHS Server going to Shutdown
//        /// </summary>
//        public static void UnInstall()
//        {
//            if (PerformanceCounterCategory.Exists(PERFORMANCE_CATEGORY_NAME))
//            {
//                PerformanceCounterCategory.Delete(PERFORMANCE_CATEGORY_NAME);
//            }
//        }
//        /// <summary>
//        /// Service TCP Port
//        /// </summary>
//        private static bool isInitialized = false;
//        public const string PERFORMANCE_CATEGORY_NAME = "HSF Business Hosting Service";
//        public const string TOTAL = "_Total";

//        #region QueryPerformanceCounter
//        ///// <summary>
//        ///// Imports the <code>QueryPerformanceFrequency</code> method into the class. The method is used to measure the current
//        ///// tickcount of the system.
//        ///// </summary>
//        ///// <param name="ticks">current tick count</param>
//        //[DllImport("Kernel32.dll")]
//        //public static extern void QueryPerformanceCounter(ref long ticks);
//        #endregion

//        #region Counters
//        /// <summary>
//        /// Counter for counting total number of operations
//        /// </summary>
//        private static PerformanceCounter _TotalOperations;
//        public static PerformanceCounter TotalOperations { get { return _TotalOperations; } }

//        /// <summary>
//        /// Counter for counting number of operations per second
//        /// </summary>
//        private static PerformanceCounter _OperationsPerSecond;
//        public static PerformanceCounter OperationsPerSecond { get { return _OperationsPerSecond; } }
//        /// <summary>
//        /// Counter for counting duration averages
//        /// </summary>
//        private static PerformanceCounter _AverageDuration;
//        public static PerformanceCounter AverageDuration { get { return _AverageDuration; } }

//        /// <summary>
//        /// Counter for counting duration averages base
//        /// </summary>
//        private static PerformanceCounter _AverageDurationBase;
//        public static PerformanceCounter AverageDurationBase { get { return _AverageDurationBase; } }

//        /// <summary>
//        /// Counter for counting total number of operations
//        /// </summary>
//        private static PerformanceCounter _TotalExceptions;
//        public static PerformanceCounter TotalExceptions { get { return _TotalExceptions; } }

//        /// <summary>
//        /// Counter for counting number of operations per second
//        /// </summary>
//        private static PerformanceCounter _ExceptionsPerSecond;
//        public static PerformanceCounter ExceptionsPerSecond { get { return _ExceptionsPerSecond; } }

//        /// <summary>
//        /// Counter for counting number of RequestQueue
//        /// </summary>
//        private static PerformanceCounter _RequestQueueLength;
//        public static PerformanceCounter RequestQueueLength { get { return _RequestQueueLength; } }
//        #endregion

//        #region Total Counter
//        private static PerformanceCounter T_TotalOperations = null;
//        private static PerformanceCounter T_OperationsPerSecond = null;
//        private static PerformanceCounter T_AverageDuration = null;
//        private static PerformanceCounter T_AverageDurationBase = null;
//        private static PerformanceCounter T_TotalExceptions = null;
//        private static PerformanceCounter T_ExceptionsPerSecond = null;


//        //private static PerformanceCounter T_RequestQueueLength = null;
//        #endregion

//        static PerformanceCounterUtil()
//        {
//            if (!isInitialized)
//            {
//                Initialize();
//            }
//        }
//        public static void Initialize()
//        {
//            #region Create CounterCategory

//            if (!PerformanceCounterCategory.Exists(PERFORMANCE_CATEGORY_NAME))
//            {
//                try
//                {
//                    CounterCreationDataCollection counters = new CounterCreationDataCollection();


//                    CounterCreationData totalOps = new CounterCreationData();
//                    totalOps.CounterName = "Calls";
//                    totalOps.CounterHelp = "이 {0}에 대한 호출 수입니다. ";
//                    totalOps.CounterType = PerformanceCounterType.NumberOfItems32;
//                    counters.Add(totalOps);



//                    /// Calls , 이 서비스에 대한 호출 수입니다. 
//                    /// Calls Duration
//                    /// Calls Per Second
//                    /// Calls Outstanding
//                    /// Calls Failed
//                    /// Calls Failed Per Second
//                    /// Calls Faulted
//                    /// Calls Faulted Per Second
//                    /// Calls Duration
//                    /// 
//                    /// 
//                    /// **DB Componet 성능 카운터 발생**
//                    /// Transaction 
//                    /// Transaction Duration
//                    /// Transaction Exception
//                    /// Transaction Exception Per Second
                    
//                    // 총 서비스 회수
//                    // 1. counter for counting totals: PerformanceCounterType.NumberOfItems32
//                    CounterCreationData totalOps = new CounterCreationData();
//                    totalOps.CounterName = "# HSFServices operations executed";
//                    totalOps.CounterHelp = "Total number of operations executed";
//                    totalOps.CounterType = PerformanceCounterType.NumberOfItems32;
//                    counters.Add(totalOps);


//                    // 초당 서비스 수
//                    // 2. counter for counting operations per second: PerformanceCounterType.RateOfCountsPerSecond32
//                    CounterCreationData opsPerSecond = new CounterCreationData();
//                    opsPerSecond.CounterName = "# HSFServices operations / sec";
//                    opsPerSecond.CounterHelp = "Number of operations executed per second";
//                    opsPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
//                    counters.Add(opsPerSecond);

//                    // 평균 처리속도
//                    // 3. counter for counting average time per operation: PerformanceCounterType.AverageTimer32
//                    CounterCreationData avgDuration = new CounterCreationData();
//                    avgDuration.CounterName = "HSFServices average time per operation";
//                    avgDuration.CounterHelp = "Average duration per operation execution";
//                    avgDuration.CounterType = PerformanceCounterType.AverageTimer32;
//                    counters.Add(avgDuration);

//                    // 4. base counter for counting average time per operation: PerformanceCounterType.AverageBase
//                    CounterCreationData avgDurationBase = new CounterCreationData();
//                    avgDurationBase.CounterName = "HSFServices average time per operation base";
//                    avgDurationBase.CounterHelp = "HSFServices Average duration per operation execution base";
//                    avgDurationBase.CounterType = PerformanceCounterType.AverageBase;
//                    counters.Add(avgDurationBase);


//                    // 총 오류 회수
//                    // 5. counter for counting totals: PerformanceCounterType.NumberOfItems32
//                    CounterCreationData totalErrOps = new CounterCreationData();
//                    totalErrOps.CounterName = "# HSFServices Exceptions";
//                    totalErrOps.CounterHelp = "Total number of Exceptions";
//                    totalErrOps.CounterType = PerformanceCounterType.NumberOfItems32;
//                    counters.Add(totalErrOps);

//                    // 초당 오류 회수
//                    // 6. counter for counting operations per second: PerformanceCounterType.RateOfCountsPerSecond32
//                    CounterCreationData opsErrorPerSecond = new CounterCreationData();
//                    opsErrorPerSecond.CounterName = "# HSFServices Exceptions / sec";
//                    opsErrorPerSecond.CounterHelp = "Number of Exceptions per second";
//                    opsErrorPerSecond.CounterType = PerformanceCounterType.RateOfCountsPerSecond32;
//                    counters.Add(opsErrorPerSecond);

//                    //// 총 서비스 회수
//                    //// 1. counter for counting totals: PerformanceCounterType.NumberOfItems32
//                    //CounterCreationData totalRequestQueue = new CounterCreationData();
//                    //totalRequestQueue.CounterName = "# HSFServices Request Queue Length";
//                    //totalRequestQueue.CounterHelp = "number of Request Queue";
//                    //totalRequestQueue.CounterType = PerformanceCounterType.NumberOfItems32;
//                    //counters.Add(totalRequestQueue);


//                    // create new category with the counters above
//                    PerformanceCounterCategory.Create(
//                        PERFORMANCE_CATEGORY_NAME
//                        , "HSF Business Hosting Service Counters"
//                        , PerformanceCounterCategoryType.MultiInstance
//                        , counters
//                        );
//                }
//                catch (Exception)
//                {
//                }
//            }
//            #endregion

//            #region Create Counter


//            #region total counter
//            T_TotalOperations = CreateCounterTotal(PERFORMANCE_CATEGORY_NAME, "# HSFServices operations executed", TOTAL);
//            #endregion

//            #region instance counter
//            string instancName = string.Format("{0}", Port);
//            _TotalOperations = CreateCounter(PERFORMANCE_CATEGORY_NAME, "# HSFServices operations executed", instancName);
//            #endregion

//            #endregion

//            isInitialized = true;
//        }

//        private static PerformanceCounter CreateCounterTotal(
//            string categoryName,
//            string counterName,
//            string instanceName)
//        {
//            PerformanceCounter counter = null;
//            bool isExistInstance = PerformanceCounterCategory.InstanceExists(instanceName, categoryName);

//            counter =
//                new PerformanceCounter(
//                         categoryName
//                         , counterName
//                         , instanceName
//                         , ".");
//            counter.ReadOnly = false;
//            if (!isExistInstance)
//            {
//                counter.RawValue = 0;
//            }

//            CounterList.Add(counter);

//            return counter;
//        }

//        private static PerformanceCounter CreateCounter(
//            string categoryName,
//            string counterName,
//            string instanceName)
//        {
//            PerformanceCounter counter = null;

//            counter =
//                new PerformanceCounter(
//                         categoryName
//                         , counterName
//                         , instanceName
//                         , ".");
//            counter.ReadOnly = false;
//            counter.RawValue = 0;

//            CounterList.Add(counter);
//            return counter;
//        }
//        public static void Increment(PerformanceCounterKind type)
//        {
//            InstancePerformanceCounterList[type].Increment();
//            TotalPerformanceCounterList[type].Increment();
//        }

//        public static void IncrementBy(PerformanceCounterKind type, long value)
//        {
//            InstancePerformanceCounterList[type].IncrementBy(value);
//            TotalPerformanceCounterList[type].IncrementBy(value);
//        }

//    }
//}
